package Revise;

import java.util.Arrays;

public class TwoSearch {
    public void test(){
        System.out.println("测试空对象可不可以代用实例方法");
    }
    public static void main(String[] args) {
        int[] arr = {2, 7, 9, 4, 6, 3, 5};
        Arrays.sort(arr);
        int res = search(arr, 6);
        System.out.println(res);
        TwoSearch a=null;
        a.test();//空指针异常

    }

    public static int search(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] > target) {
                right = mid - 1;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
